﻿using System;
using System.Collections.Generic;
using System.Text;
using PostSharp.Aspects;

namespace EasyDLL.AOP.Attributes
{
    [Serializable]
    public class ExceptionAttribute : OnExceptionAspect
    {
        public override void OnException(MethodExecutionArgs eventArgs)
        {
            string arguments = string.Empty;
            foreach (object argument in eventArgs.Arguments)
            {
                arguments += argument.ToString();
            }
            string msg = string.Format(@"Method {0} occured a exception:
=====================================================================================================================================
message:        {1}
StackTrace:     {2}
Arguments:      {3}
=====================================================================================================================================
"
, eventArgs.Method.Name, eventArgs.Exception.Message, eventArgs.Exception.StackTrace, eventArgs.Exception.TargetSite, arguments);
            Logging.LoggingWrapper.Instance.LogError(msg);
            //抛出异常，防止异常传递
            eventArgs.FlowBehavior = FlowBehavior.RethrowException;
        }
    }
}
